const formatTime = date => {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()
  const hour = date.getHours()
  const minute = date.getMinutes()
  const second = date.getSeconds()

  return [year, month, day].map(formatNumber).join('/') + ' ' + [hour, minute, second].map(formatNumber).join(':')
}

const formatNumber = n => {
  n = n.toString()
  return n[1] ? n : '0' + n
}

function brightenKeyword(val, keyword) {
  const Reg = new RegExp(keyword, 'i');
  let key = [];
  let res = '';
  if (val) {
    key = val.match(Reg)
    res = val.replace(Reg, `<span style="color: #D1AA66;">${key[0]}</span>`);
    return res;
  }
}

function countDown(time) {
  // console.log(time,'开始时间');
  // time.replace(/-/, "/");
  var nowDate = new Date();
  // console.log(time.replace(/-/g, "/"),'ppppppppppppppppppp')；
  var EndDate = new Date(time.replace(/-/g, "/"));
  // console.log(nowDate,EndDate,'最新时间');
  var dateDiff = EndDate.getTime() - nowDate.getTime();
  // console.log(EndDate.getTime(),nowDate.getTime(),'最新时间相差得秒数');
  if(dateDiff<0){
    return -1
  }
  // console.log(dateDiff,'相差的时间 秒为单位');
  var days = Math.floor(dateDiff / (24 * 3600 * 1000)) //计算出天数 
  //计算出小时数 
  var leave1 = dateDiff % (24 * 3600 * 1000)  //计算天数后剩余的毫秒数 
  var hours = Math.floor(leave1 / (3600 * 1000))
  //计算相差分钟数 
  var leave2 = leave1 % (3600 * 1000)    //计算小时数后剩余的毫秒数 
  var minutes = Math.floor(leave2 / (60 * 1000))
  //计算相差秒数 
  var leave3 = leave2 % (60 * 1000)   //计算分钟数后剩余的毫秒数 
  var seconds = Math.round(leave3 / 1000)
  //调用checkTime方法将数字小于10的在前面补0
  days = checkTime(days);
  hours = checkTime(hours);
  minutes = checkTime(minutes);
  seconds = checkTime(seconds);
  // console.log(days, hours, minutes, seconds);
  return {
    days, hours, minutes, seconds
  }
}

function checkTime(i){
  if(i<10&&i!=0){
      i="0"+i;
  }
      return i;
  }
  function starThemeStyle(star) {
    if(star.themeId == 1){
      star.tagColor = '#EC8928';
      star.tagBack = '#F9D268';
      star.defaultIcon = 'https://story-star.oss-cn-beijing.aliyuncs.com/upload/2023-09-21/k3ikPQ_main_item_icon1.png';
    } else if(star.themeId == 2){
      star.tagColor = '#5CCB2B';
      star.tagBack = '#B1F25F';
      star.defaultIcon = 'https://story-star.oss-cn-beijing.aliyuncs.com/upload/2023-09-21/tDedE5_main_item_icon2.png';
    } else if(star.themeId == 3){
      star.tagColor = '#3E7EF1';
      star.tagBack = '#83D6FC';
      star.defaultIcon = 'https://story-star.oss-cn-beijing.aliyuncs.com/upload/2023-09-21/a82TH5_main_item_icon3.png';
    } else {
      star.tagColor = '#EC8928';
      star.tagBack = '#F9D268';
      star.defaultIcon = 'https://story-star.oss-cn-beijing.aliyuncs.com/upload/2023-09-21/k3ikPQ_main_item_icon1.png';
    }
    return star;
  }
  function compareVersion(v1, v2) {
    v1 = v1.split('.')
    v2 = v2.split('.')
    const len = Math.max(v1.length, v2.length)
  
    while (v1.length < len) {
      v1.push('0')
    }
    while (v2.length < len) {
      v2.push('0')
    }
  
    for (let i = 0; i < len; i++) {
      const num1 = parseInt(v1[i])
      const num2 = parseInt(v2[i])
  
      if (num1 > num2) {
        return 1
      } else if (num1 < num2) {
        return -1
      }
    }
  
    return 0
  }


   const throttle =(func, limit) =>{
    let lastFunc;
    let lastRan;
    let isThrottled = false; // 新增的标志
  
    return function () {
      const context = this;
      const args = arguments;
      if (!lastRan) {
        func.apply(context, args);
        lastRan = Date.now();
        isThrottled = false;
      } else {
        isThrottled = true;
        clearTimeout(lastFunc);
        lastFunc = setTimeout(function () {
          if ((Date.now() - lastRan) >= limit) {
            if (lastFunc) {
              func.apply(context, args);
              lastRan = Date.now();
            }
            isThrottled = false;
          }
        }, limit - (Date.now() - lastRan));
      }
      return isThrottled; // 返回当前是否正在被节流
    }
  }
  
module.exports = {
  formatTime: formatTime,
  brightenKeyword: brightenKeyword,
  countDown,
  starThemeStyle,
  throttle,
  compareVersion
}